﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using BudgetManager.Domain;
using Microsoft.Phone.Shell;
using System.Windows.Navigation;
using System.Windows.Controls.Primitives;

namespace BudgetManager
{
    public partial class Goal : PhoneApplicationPage
    {
        Category _category ;
        private object okB;
        private object editB;
        private object deleteB;
        private object cancelB;
        public Goal()
        {
            InitializeComponent();
            _category = Category.ReadDefaultCategory();
            txtCategory.Text = _category.Name;
            datePickerStartDate.Value = DateTime.Now;
            datePickerEndDate.Value = DateTime.Now;
			//txtCategory.Text = "Alimentação";
            listBoxGoals.ItemsSource = AccountManager.SelectedAccount.GetGoals();

            okB = ApplicationBar.Buttons[0];
            editB = ApplicationBar.Buttons[1];
            deleteB = ApplicationBar.Buttons[2];
            cancelB = ApplicationBar.Buttons[3];
            ApplicationBar.Buttons.Clear();
            txtAlert.Text = "Alert me whem 75% of my goal is complete"; 

            Object category;
            if (PhoneApplicationService.Current.State.TryGetValue("Category", out category) && category != null)
            {
                GoalsPivot.SelectedIndex = 1;
                _category = (Category)category; // save state
                txtCategory.Text = _category.Name; //change text box
                PhoneApplicationService.Current.State["Category"] = null; // Clean memory
            }
        }        

    
        private void sliderAlert_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (txtAlert != null)
            {
                txtAlert.Text = "Alert me whem " + Convert.ToInt32(e.NewValue) +"% of my goal is complete"; 
              
            }
        }

        private void onOk(object sender, EventArgs e)
        {
            this.Focus();
            if (GoalsPivot.SelectedIndex == 1)
            {
                DateTime? startDate = datePickerStartDate.Value;
                DateTime? endDate = datePickerEndDate.Value;
                double alert = sliderAlert.Value;

                BudgetManager.Domain.Goal goal = new BudgetManager.Domain.Goal { StartDate = startDate.Value, EndDate = endDate.Value, Alert = alert, Category = _category };
                AccountManager.SelectedAccount.addGoal(goal);
            }
            NavigationService.Navigate(new Uri(string.Format("/Home.xaml?"), UriKind.Relative));

        }

        private void onCancel(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri(string.Format("/Home.xaml?"), UriKind.Relative));
        }

        private void onTapCategory(object sender, System.Windows.Input.GestureEventArgs e)
        {
            NavigationService.Navigate(new Uri(string.Format("/CategoryGoal.xaml?"), UriKind.Relative));
        }
        
        private void onDelete(object sender, EventArgs e)
        {
            if (listBoxGoals.SelectedValue != null)
            {
                AccountManager.SelectedAccount.deleteGoal(listBoxGoals.SelectedItem);
                listBoxGoals.ItemsSource = AccountManager.SelectedAccount.GetGoals();
            }
        }

        private void onchangeonPivot(object sender, PivotItemEventArgs e)
        {
            this.Focus();//esconder o teclado
            if (GoalsPivot.SelectedIndex == 0) // view/edit
            {
                ApplicationBar.Buttons.Clear();
                ApplicationBar.Buttons.Add(okB);
                ApplicationBar.Buttons.Add(editB);
                ApplicationBar.Buttons.Add(deleteB);
                ApplicationBar.Buttons.Add(cancelB);
            }
            else    //
            {
                ApplicationBar.Buttons.Clear();
                ApplicationBar.Buttons.Add(okB);
                ApplicationBar.Buttons.Add(cancelB);
            }
        }

        private void onEdit(object sender, EventArgs e)
        {
          //  if (listBoxGoals.SelectedValue != null)
           // {
            //    PhoneApplicationService.Current.State["abstractvalue"] = userControlViewValues.SelectedValue;
            //    NavigationService.Navigate(new Uri(string.Format("/EditValue.xaml?"), UriKind.Relative));
           // }
        }

        private void clean(object sender, TextCompositionEventArgs e)
        {
            txtAmount.Text = "";
        }
    

    }
}
